#include "util.hpp"
#include "level.hpp"
#include "message.hpp"
#include "format.hpp"
#include "sink.hpp"
#include <unistd.h>
/*扩展一个以时间作为入职文件滚动切换类型的日志落地模块
    1.以时间进行文件滚动，实际上是以时间段进行滚动
      实现思想:以当前系统时间，取模时间段大小，可以得到当前时间段是第几个时间段
              每次以当前系统时间取模，判断与当前文件的时间段是否一致，不一致代表不是同一个时间段
*/
//落地方向: 指定方向
enum class TimeGap {
    GAP_SECOND,
    GAP_MINUTE,
    GAP_HOUR,
    GAP_DAY,
};
class RollByTimeSink : public bitlog::LogSink {
public:
    //构造时传入文件名，并打开文件，将操作句柄管理起来
    RollByTimeSink(const std::string &pathname, TimeGap gap_type):_basename(pathname) {
        switch (gap_type)
        {
        case TimeGap::GAP_SECOND: _gap_size = 1; break;
        case TimeGap::GAP_MINUTE: _gap_size = 60; break;
        case TimeGap::GAP_HOUR: _gap_size = 3600; break;
        case TimeGap::GAP_DAY: _gap_size = 3600*24; break;  
        }
        _cur_gap = _gap_size == 1 ? bitlog::util::Date::now() : bitlog::util::Date::now() % _gap_size; //获取当前是第几个时间段
        std::string filename = createNewFile();
        bitlog::util::File::createDirectory(bitlog::util::File::path(filename));
        _ofs.open(filename, std::ios::binary | std::ios::app);
        assert(_ofs.is_open());
    }
    //将日志消息写入到标准输出,判断当前时间是否是当前文件时间段，不是则切换文件
    void log(const char *data, size_t len) {
        time_t cur = bitlog::util::Date::now();
        if((cur % _gap_size) != _cur_gap) {
            _ofs.close();
            std::string filename = createNewFile();
            bitlog::util::File::createDirectory(bitlog::util::File::path(filename));
            _ofs.open(filename, std::ios::binary | std::ios::app);
        }
        _ofs.write(data, len);
        assert(_ofs.good());
    }
    private:
        std::string createNewFile() {
             //获取系统时间，以时间来构造文件名扩展名
            time_t t = bitlog::util::Date::now();
            struct tm lt;
            localtime_r(&t, &lt);
            std::stringstream filename;
            filename << _basename;
            filename << lt.tm_year + 1900;
            filename << lt.tm_mon + 1;
            filename << lt.tm_mday;
            filename << lt.tm_hour;
            filename << lt.tm_min;
            filename << lt.tm_sec;
            filename << ".log";
            return filename.str();
        }
    private:
        std::string _basename;
        std::ofstream _ofs;
        size_t _cur_gap; //当前是第几个时间段
        size_t _gap_size; //时间段大小

};
int main()
{
    // std::cout << bitlog::util::Date::getTime() << std::endl;
    // std::string pathname = "./abc/bcd/a.txt";
    // bitlog::util::File::createDirectory(bitlog::util::File::path(pathname));
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::DEBUG) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::INFO) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::WARN) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::ERROR) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::FATAL) << std::endl;
    // std::cout << bitlog::LogLevel::toString(bitlog::LogLevel::value::OFF) << std::endl;
    bitlog::LogMsg msg(bitlog::LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    bitlog::Formatter fmt;
    std::string str = fmt.format(msg);
    //std::cout << str << std::endl;
    // bitlog::LogSink::ptr stdout_lsp = bitlog::SinkFactory::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp = bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr roll_lsp = bitlog::SinkFactory::create<bitlog::RollBySizeSink>("./logfile/roll-", 1024*1024);
    bitlog::LogSink::ptr time_lsp = bitlog::SinkFactory::create<RollByTimeSink>("./logfile/roll-", TimeGap::GAP_SECOND);
    time_t old = bitlog::util::Date::now();
    while(bitlog::util::Date::now() < old + 5) {
        time_lsp->log(str.c_str(), str.size());
        usleep(1000);
    }
    // stdout_lsp->log(str.c_str(), str.size());
    // file_lsp->log(str.c_str(), str.size());
    // size_t cursize = 0;
    // size_t count = 0;
    // while(cursize < 1024 * 1024 * 10) {
    //     std::string tmp = str + std::to_string(count++); // 换行在str末尾处
    //     roll_lsp->log(tmp.c_str(), tmp.size());
    //     cursize += tmp.size();
    // }


}